import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class tw {
    public List<Integer> preorderTraversal(TreeNode root) {
        //思路太牛了 注意先找右的一定要先弹出
        List<Integer> list=new LinkedList<>();
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                list.add(stack.peek().val);
                cur=cur.left;
            }
            TreeNode retun=stack.pop();
            cur=retun.right;
        }
        return list;
    }
}
